package view;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;

import org.jdesktop.xswingx.PromptSupport;

import types.ConsoleType;
import controller.Console;
import dataLayer.DAOAbsence;
import export.ExportDay;
import export.ExportMonth;
import export.ExportWeek;

public class PanelExport extends JPanel {

	private static PanelExport single;

	Calendar calendar = Calendar.getInstance();

	// ArrayList<Teacher> teacherList = new ArrayList<Teacher>(College
	// .getInstance().getAllactiveTeachers());

	// private JButton JBexTeacher = new JButton("Export teacher");
	int monthNumber = 0;
	String monthName = null;
	int monthYear = 0;
	private JButton JBexYear = new JButton("Export jaar");
	private JButton JBexMonth = new JButton("Export maand");
	private JButton JBexWeek = new JButton("Exporteer deze week");
	private JButton JBexDay = new JButton("Export dag");

	private JLabel reasonAdd = new JLabel("Voeg reden toe");
	private JCheckBox checkReason = new JCheckBox();
	private JLabel noteAdd = new JLabel("Voeg notitie toe");
	private JCheckBox checkNote = new JCheckBox();

	private JButton JBselectPath = new JButton("browse");
	private JTextField JTPath = new JTextField();

	// private JComboBox<String> teacherCombo;
	private JSpinner yearSpinner = new JSpinner();
	private JSpinner monthSpinner = new JSpinner();
	private JSpinner daySpinner = new JSpinner();

	public PanelExport() {
		setBounds(0, 0, 860, 720);
		setLayout(null);

		// DefaultComboBoxModel<String> defaultTeacherCombo = new
		// DefaultComboBoxModel<>();
		// teacherCombo = new JComboBox<>(defaultTeacherCombo);
		// for (int n = 0; n < teacherList.size(); n++) {
		// String code = teacherList.get(n).getTeacher_code();
		// teacherCombo.addItem(code);
		// }
		// Maak een JSpinner aan met het huidige jaar
		Date today = new Date();

		yearSpinner = new JSpinner(new SpinnerDateModel(today, null, null,
				Calendar.MONTH));
		JSpinner.DateEditor de = new JSpinner.DateEditor(yearSpinner, "yyyy");
		yearSpinner.setEditor(de);

		yearSpinner.setVisible(true);

		// Maak een JSpinner met de huidige maand
		monthSpinner = new JSpinner(new SpinnerDateModel(today, null, null,
				Calendar.MONTH));
		JSpinner.DateEditor de1 = new JSpinner.DateEditor(monthSpinner,
				"MM/yyyy");
		monthSpinner.setEditor(de1);

		monthSpinner.setVisible(true);

		// Maak een JSpinner met het huidige weeknummer

		// create Calendar instance
		Calendar now = Calendar.getInstance();

		int weekOfYear = now.get(Calendar.WEEK_OF_YEAR);

		System.out.println(monthNumber);

		// Week spinner not in use
		// SpinnerModel model = new SpinnerNumberModel(weekOfYear, 1, 53, 1);
		// JSpinner weekSpinner = new JSpinner(model);

		daySpinner.setVisible(true);

		// Maak een JSpinner met de huidige dag
		daySpinner = new JSpinner(new SpinnerDateModel(today, null, null,
				Calendar.MONTH));
		JSpinner.DateEditor de3 = new JSpinner.DateEditor(daySpinner,
				"dd/MM/yy");
		daySpinner.setEditor(de3);

		daySpinner.setVisible(true);

		// teacherCombo.setBounds(100, 80, 100, 30);
		daySpinner.setBounds(150, 180, 80, 30);
		// weekSpinner.setBounds(150, 180, 80, 30);
		monthSpinner.setBounds(150, 280, 80, 30);
		yearSpinner.setBounds(150, 330, 80, 30);

		// JBexTeacher.setBounds(300, 80, 140, 30);
		JBexDay.setBounds(300, 180, 140, 30);

		checkNote.setBounds(150, 140, 30, 30);
		noteAdd.setBounds(170, 140, 100, 30);

		checkReason.setBounds(150, 120, 30, 30);
		reasonAdd.setBounds(170, 120, 100, 30);
		JBexWeek.setBounds(300, 230, 140, 30);
		JBexMonth.setBounds(300, 280, 140, 30);
		JBexYear.setBounds(300, 330, 140, 30);
		JBselectPath.setBounds(450, 70, 70, 30);
		JTPath.setBounds(150, 70, 300, 30);

		// Zie voor prompt support:
		// http://code.google.com/p/xswingx/wiki/GettingStarted
		PromptSupport.setPrompt("Selecteer een locatie", JTPath);

		JBselectPath.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String path = promptForFolder(getParent());

				JTPath.setText(path);

			}
		});

		JBexDay.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (JTPath.getText().equals("")) {
					String message = "Selecteer een locatie op uw computer";
					ConsoleType type = ConsoleType.ERROR;
					Console.getInstance().setConsole(message, type);
				} else {
					Date date = (Date)daySpinner.getValue();
					JBexWeek.addActionListener(new ExportDay(JTPath.getText()
							+ "/DAS_Dag_" + date + "_export.pdf",
							date));
				
					

				}
			}
		});
		JBexWeek.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if (JTPath.getText().equals("")) {

					String message = "Selecteer een locatie op uw computer";
					ConsoleType type = ConsoleType.ERROR;
					Console.getInstance().setConsole(message, type);
				} else {
					JBexWeek.addActionListener(new ExportWeek(JTPath.getText()
							+ "/DAS_Week_" + weekOfYear + "_export.pdf",
							weekOfYear));

					// new ExportWeek(JTPath.getText() + "/DAS_Week_"
					// + weekSpinner.getValue() + "_export.pdf");
				}

			}
		});
		JBexMonth.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// System.out.println(getMonthNumber());
				if (JTPath.getText().equals("")) {
					String message = "Selecteer een locatie op uw computer";
					ConsoleType type = ConsoleType.ERROR;
					Console.getInstance().setConsole(message, type);
				} else {
					JBexMonth.addActionListener(new ExportMonth(JTPath
							.getText()
							+ "/DAS_Week_"
							+ weekOfYear
							+ "_export.pdf", weekOfYear));

					// new ExportWeek(JTPath.getText() + "/DAS_Week_"
					// + weekSpinner.getValue() + "_export.pdf");
				}

			}
		});

		// JBexTeacher.addActionListener(new ExportTeacher());

		// JBexYear.addActionListener(new ExportYear(JTPath.getText()));
		// JBexMonth.addActionListener(new ExportMonth(JTPath.getText()));
		// JBexDay.addActionListener(new ExportDay(JTPath.getText()));

		add(JTPath);
		add(JBselectPath);
		// add(teacherCombo);

		add(checkNote);
		add(noteAdd);
		add(checkReason);
		add(reasonAdd);
		add(yearSpinner);
		add(monthSpinner);
		// add(weekSpinner);
		add(daySpinner);

		// add(JBexTeacher);
		add(JBexYear);
		add(JBexMonth);
		add(JBexWeek);
		add(JBexDay);
	}

	// public int getMonthNumber(){
	// Object month = monthSpinner.getValue();
	// System.out.println(month);
	// String monthString = month.toString();
	// if (monthString.contains("Jan")){
	// monthNumber = 1;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Feb")){
	// monthNumber = 2;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Mar")){
	// monthNumber = 3;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Apr")){
	// monthNumber = 4;
	// monthName = "Jan";
	// }
	// if (monthString.contains("May")){
	// monthNumber = 5;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Jun")){
	// monthNumber = 6;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Jul")){
	// monthNumber = 7;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Aug")){
	// monthNumber = 8;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Sep")){
	// monthNumber = 9;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Okt")){
	// monthNumber = 10;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Nov")){
	// monthNumber = 11;
	// monthName = "Jan";
	// }
	// if (monthString.contains("Dec")){
	// monthNumber = 12;
	// monthName = "Jan";
	// }
	// if (monthString.contains("2014")){
	// monthYear = 2014;
	// }
	// if (monthString.contains("2015")){
	// monthYear = 2015;
	// }
	// if (monthString.contains("2016")){
	// monthYear = 2016;
	// }
	// if (monthString.contains("2017")){
	// monthYear = 2017;
	// }
	// if (monthString.contains("2018")){
	// monthYear = 2018;
	// }
	// if (monthString.contains("2019")){
	// monthYear = 2019;
	// }
	// if (monthString.contains("2020")){
	// monthYear = 2020;
	// }
	// if (monthString.contains("2021")){
	// monthYear = 2021;
	// }
	// if (monthString.contains("2022")){
	// monthYear = 2022;
	// }
	// if (monthString.contains("2023")){
	// monthYear = 2023;
	// }
	// if (monthString.contains("2024")){
	// monthYear = 2024;
	// }
	// if (monthString.contains("2025")){
	// monthYear = 2025;
	// }
	//
	// return monthNumber;
	//
	// }

	public JCheckBox getCheckReason() {
		return checkReason;
	}

	public void setCheckReason(JCheckBox checkReason) {
		this.checkReason = checkReason;
	}

	public JTextField getJTPath() {
		return JTPath;
	}

	public void setJTPath(JTextField jTPath) {
		JTPath = jTPath;
	}

	public String promptForFolder(Component parent) {
		JFileChooser fc = new JFileChooser();

		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		if (fc.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
			return fc.getSelectedFile().getAbsolutePath();
		}

		return null;
	}

	public static PanelExport getInstance() {
		if (single == null) {
			single = new PanelExport();
		}
		return single;

	}

}
